﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SegmentDisplay.Common
{
    /// <summary>
    /// Base class for all segment controls
    /// </summary>
    public class SegmentBase : Control
    {
        #region Brushes

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        public Brush LightOn
        {
            get { return (Brush)GetValue(LightOnProperty); }
            set { SetValue(LightOnProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the source.
        /// </summary>
        public static DependencyProperty LightOnProperty =
            DependencyProperty.Register("LightOn", typeof(Brush), typeof(SegmentBase), new PropertyMetadata(SegmentBrushes.GreenLightOn));

        /// <summary>
        /// Gets or sets the source.
        /// </summary>
        public Brush LightOff
        {
            get { return (Brush)GetValue(LightOffProperty); }
            set { SetValue(LightOffProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the source.
        /// </summary>
        public static DependencyProperty LightOffProperty =
            DependencyProperty.Register("LightOff", typeof(Brush), typeof(SegmentBase), new PropertyMetadata(SegmentBrushes.GreenLightOff));

        protected Brush GetBrush(bool value)
        {
            return value ? LightOn : LightOff;
        }
        #endregion

        #region Management of serment
        /// <summary>
        /// Property for switch on/off of indicator
        /// </summary>
        public bool IsSwitchOn
        {
            get { return (bool)GetValue(IsSwitchOnProperty); }
            set { SetValue(IsSwitchOnProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the source.
        /// </summary>
        public static DependencyProperty IsSwitchOnProperty =
            DependencyProperty.Register("IsSwitchOn", typeof(bool), typeof(SegmentBase), new PropertyMetadata(false, new PropertyChangedCallback(IsSwitchOnChanged)));

        /// <summary>
        /// Function on change IsSwitchOn property 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void IsSwitchOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SegmentBase sh = (SegmentBase)d;
            sh.OnIsSwitchOnChanged((bool)e.NewValue);
        }

        /// <summary>
        /// Virtual function for overriding a behavior on change IsSwitchOn property in descendant type
        /// </summary>
        /// <param name="newValue"></param>
        public virtual void OnIsSwitchOnChanged(bool newValue)
        {
        }
        #endregion

        #region Contructor

        /// <summary>
        /// Static constructor
        /// </summary>
        static SegmentBase()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SegmentBase), new FrameworkPropertyMetadata(typeof(SegmentBase)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public SegmentBase()
        {
            this.DefaultStyleKey = typeof(SegmentBase);
        }
        #endregion
    }
}
